package zh;


import java.awt.Color;
import java.awt.geom.Point2D;
import java.io.File;
import java.io.IOException;
import java.util.logging.ConsoleHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
import robocode.*;
import zh.logging.MyLogger;


/**
 * MyFirstRobot - a sample robot by Mathew Nelson
 *
 * Moves in a seesaw motion, and spins the gun around at each end
 */
public class UnderDog extends AdvancedRobot
{
    private static MyLogger logger = MyLogger.getLogger(UnderDog.class.getName());
    private final RobotBrain myBrain;
    private boolean hasPlayedClip = false;
    public UnderDog()
    {
        super();
        myBrain = RobotBrain.getInstance(this);
    }
    /**
     * MyFirstRobot's run method - Seesaw
     */
    public void run()
    {
        myBrain.reset();
        if (this.getOthers() == 1 && !hasPlayedClip)
        {
            hasPlayedClip = true;
//            playClip();
        }
        this.setRadarColor(Color.red);
        this.setGunColor(Color.yellow);
        this.setBodyColor(Color.yellow);
        this.setScanColor(Color.yellow);
        this.setBulletColor(Color.yellow);
        this.setAdjustGunForRobotTurn(true);
    
        while (true)
        {
            myBrain.makeDecision();
        }
    }

    public boolean isGoingForward()
    {
        return (getDistanceRemaining() >= 0.0 ? true : false);
    }
    
    public void onScannedRobot(ScannedRobotEvent event)
    {
        myBrain.processEvent(event);
    }
    
    public void onHitByBullet(HitByBulletEvent event)
    {
        myBrain.processEvent(event);
    }
    
    public void onHitRobot(HitRobotEvent event)
    {
        myBrain.processEvent(event);
    }
    
    public void onHitWall(HitWallEvent event)
    {
        myBrain.processEvent(event);
    }
    
    public void onBulletMissed(BulletMissedEvent event)
    {
        myBrain.processEvent(event);
    }

    public void onBulletHit(BulletHitEvent event)
    {
        myBrain.processEvent(event);
    }
    
    public void onCustomEvent(CustomEvent event)
    {
        myBrain.processEvent(event);
    }
    
    public void onRobotDeath(RobotDeathEvent event)
    {
        myBrain.processEvent(event);
    }
    
    public void onDeath(DeathEvent event)
    {
        myBrain.processEvent(event);
    }
    public void onWin(WinEvent event)
    {
        myBrain.processEvent(event);
    }
    
    public void onSkippedTurn(SkippedTurnEvent event)
    {
       System.out.println("WHOOPSTakinto   LONG!");
    }
    
    public boolean doOtherOpponentsExist()
    {
        return (0 < getOthers());
    }
    
    public Point2D getLocation()
    {
        return new Point2D.Double(getX(), getY());
    }
    
    private static final int EXTERNAL_BUFFER_SIZE = 128000;
    private void playClip()
    {
        try
        {
            File dataDir = getDataDirectory();
            File file = new File(dataDir, "dangw.wav");
            AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(file);
            AudioFormat	audioFormat = audioInputStream.getFormat();
            DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
			SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
			line.open(audioFormat);
            line.start();

            int	nBytesRead = 0;
            byte[]	abData = new byte[EXTERNAL_BUFFER_SIZE];
            while (nBytesRead != -1)
            {
                try
                {
                    nBytesRead = audioInputStream.read(abData, 0, abData.length);
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
                if (nBytesRead >= 0)
                {
                    int	nBytesWritten = line.write(abData, 0, nBytesRead);
                }
            }
            line.drain();
            line.close();
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }
    
    
    public void onPaint(java.awt.Graphics2D g)
    {
        myBrain.onPaint(g);
    }
}
